package com.neusoft.SPNursingHome.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.SPNursingHome.dao.LevelItemDao;
import com.neusoft.SPNursingHome.po.CareItem;
import com.neusoft.SPNursingHome.po.CareLevel;
import com.neusoft.SPNursingHome.po.LevelItem;
import com.neusoft.SPNursingHome.po.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;

@Service
public class LevelItemServiceImpl extends ServiceImpl<LevelItemDao, LevelItem> implements LevelItemService {
    @Autowired
    private LevelItemDao levelItemDao;

    @Override
    public ResponseBean<Integer> addLevelItems(@RequestBody List<LevelItem> levelItems) {
        int result = this.saveBatch(levelItems) ? levelItems.size() : 0;
        if (result > 0) {
            return new ResponseBean<>(result);
        } else {
            return new ResponseBean<>(500, "添加失败");
        }
    }

    @Override
    public ResponseBean<Integer> deleteLevelItem(Integer careLevelId, Integer careItemId) {
        int result = levelItemDao.deleteLevelItem(careLevelId, careItemId);
        if (result > 0) {
            return new ResponseBean<>(result);
        } else {
            return new ResponseBean<>(500, "删除级别下的项目失败");
        }
    }

    @Override
    public ResponseBean<List<CareItem>> getCareItemsByCareLevelIdNP(Integer careLevelId) {
        System.out.println("调试：查询护理级别下的显示且开启护理项目");
        if (careLevelId == null) {
            return new ResponseBean<>(500, "护理级别ID不能为空");
        }
        List<CareItem> careItems = levelItemDao.selectCareItemsByCareLevelIdNP(careLevelId);

//        ResponseBean<List<CareItem>> responseBean = null;
//        if (careItems == null || careItems.isEmpty() || careItems.stream().allMatch(Objects::isNull)) {
//            responseBean =  new ResponseBean<>(404, "未找到该护理级别下的活跃护理项目");
//        } else {
//            responseBean = new ResponseBean<>(careItems);
//        }
//        return responseBean;
        return new ResponseBean<>(careItems);
    }

    @Override
    public ResponseBean<IPage<CareItem>> getCareItemsByCareLevelId(Integer careLevelId, long page, long size) {


        System.out.println("调试：查询护理级别下的显示且开启护理项目");
        if (careLevelId == null) {
            return new ResponseBean<>(500, "护理级别ID不能为空");
        }
        // 计算分页的偏移量
        long offset = (page - 1) * size;

        // 调用分页查询方法
        List<CareItem> careItems = levelItemDao.selectCareItemsByCareLevelId(careLevelId, offset, size);

        // 查询总记录数
        int total = levelItemDao.countCareItemsByCareLevelId(careLevelId);

        // 将查询结果封装到分页对象中
        IPage<CareItem> iPage = new Page<>(page, size);
        iPage.setRecords(careItems);
        iPage.setTotal(total);
        System.out.println("total"+iPage.getTotal()+"pages"+iPage.getPages());

//        ResponseBean<IPage<CareItem>> responseBean = null;
//        if (careItems == null || careItems.isEmpty() || careItems.stream().allMatch(Objects::isNull)) {
//            responseBean =  new ResponseBean<>(404, "未找到该护理级别下的活跃护理项目");
//        } else {
//            responseBean = new ResponseBean<>(iPage);
//        }
        return new ResponseBean<>(iPage);
    }
}
